﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace BeeldbankMVCTools
{
    public class MediaObjectTools
    {
        public static string Hoogte(string inputString)
        {
            try
            {
                string tempString = inputString.Substring(inputString.IndexOf("Hoogte:</strong> ") + 17, inputString.Length - inputString.IndexOf("Hoogte:</strong> ") - 17);
                tempString = tempString.Substring(0, tempString.IndexOf("<br />") - 1);
                int Hoogte = Convert.ToInt32(Convert.ToDouble(tempString, new CultureInfo("en-US")));
                
                return Hoogte.ToString();
            }
            catch
            {
                return "";
            }
        }
        public static string Hoogte_fromString(string inputString)
        {
            try
            {
                string tempString = inputString;
                int Hoogte = Convert.ToInt32(Convert.ToDouble(tempString, new CultureInfo("en-US")));

                return Hoogte.ToString();
            }
            catch
            {
                return "";
            }
        }

        public static string convertToDegree(string stringDMS)
        {
            string[] DMS = stringDMS.Split(' ');

            Double Degrees = Convert.ToDouble(DMS[0]);
            Double Minutes = Convert.ToDouble(DMS[1]);

            string[] AllSeconds = DMS[2].Split('.');

            Double Seconds = Convert.ToDouble(AllSeconds[0]);
            return (Degrees + (Minutes / 60) + (Seconds / 3600)).ToString();
        } 

        public static string Latitude(string inputString)
        {
            try
            {
                string tempString = inputString.Substring(inputString.IndexOf("GPS</abbr>:</strong> ") + 21, inputString.Length - inputString.IndexOf("GPS</abbr>:</strong> ") - 21);
                tempString = tempString.Substring(0, tempString.IndexOf("N,"));
                string DegString = tempString.Substring(0, tempString.IndexOf(" "));
                string MinString = tempString.Substring(tempString.IndexOf(" ") + 1, tempString.Length - tempString.IndexOf(" ") - 1);
                double Latitude = Convert.ToInt32(DegString) + Convert.ToDouble(MinString, new CultureInfo("en-US")) / 60;
                string resultString = Latitude.ToString();
                if (resultString.Length > 9)
                    resultString = resultString.Substring(0, 9);

                return resultString;
            }
            catch
            {
                return "";
            }
        }
        public static string Lat_fromString(string inputString)
        {
            string resultString = "";
            try
            {
                string tempString = inputString;
                string DegString = tempString.Substring(0, tempString.IndexOf(" "));
                string MinString = tempString.Substring(tempString.IndexOf(" ") + 1, tempString.Length - tempString.IndexOf(" ") - 1);
                double Latitude = Convert.ToInt32(DegString) + Convert.ToDouble(MinString, new CultureInfo("en-US")) / 60;
                resultString = Latitude.ToString();
            }
            catch
            {
                try
                {
                    resultString = convertToDegree(inputString);
                }
                catch
                {
                }
            }
            if (resultString.Length > 6)
                resultString = resultString.Substring(0, 6);

            return resultString;
        }

        public static string Longitude(string inputString)
        {
            try
            {
                string tempString = inputString.Substring(inputString.IndexOf("N, ") + 3, inputString.Length - inputString.IndexOf("N, ") - 3);
                tempString = tempString.Substring(0, tempString.IndexOf("E.<br />"));
                string DegString = tempString.Substring(0, tempString.IndexOf(" "));
                string MinString = tempString.Substring(tempString.IndexOf(" ") + 1, tempString.Length - tempString.IndexOf(" ") - 1);
                double Longitude = Convert.ToInt32(DegString) + Convert.ToDouble(MinString,new CultureInfo("en-US" )) / 60;
                string resultString = Longitude.ToString();
                if (resultString.Length > 6)
                    resultString = resultString.Substring(0, 6);

                return resultString;
            }
            catch
            {
                return "";
            }
        }
        public static string Long_fromString(string inputString)
        {
            string resultString = "";
            try
            {
                string tempString = inputString;
                string DegString = tempString.Substring(0, tempString.IndexOf(" "));
                string MinString = tempString.Substring(tempString.IndexOf(" ") + 1, tempString.Length - tempString.IndexOf(" ") - 1);
                double Longitude = Convert.ToInt32(DegString) + Convert.ToDouble(MinString, new CultureInfo("en-US")) / 60;
                resultString = Longitude.ToString();
            }
            catch
            {
                try
                {
                    resultString = convertToDegree(inputString);
                }
                catch
                {
                }
            }
            if (resultString.Length > 6)
                resultString = resultString.Substring(0, 6);

            return resultString;
        }

        public static string Capitalize(string inputString, string ErrorString)
        {
            // Ensure that comma's are followed by spaces.
            string[] split = inputString.Split(new Char[] { ',' });
            StringBuilder TotalString = new StringBuilder();
            foreach (string s in split)
            {
                TotalString.Append(s.Trim());
                TotalString.Append(", ");
            }
            inputString = " " + TotalString.ToString().Substring(0, TotalString.Length - 2);

            // Remove double spaces
            inputString = inputString.Replace("  ", " ");
            // Remove double spaces
            inputString = inputString.Replace("  ", " ");

            try
            {
                // Ensure that <br /> is followed by an uppercase.
                // This automically also capitalizes the string
                string[] split2 = inputString.Split(new string[] { "<br />" }, StringSplitOptions.None);
                //string[] split2 = inputString.Split(new string[] { "<br />" }, StringSplitOptions.RemoveEmptyEntries);
                TotalString = new StringBuilder();
                foreach (string s in split2)
                {
                    string trimmedString = s.Trim();

                    if (trimmedString.Length > 3)
                    {
                        // When details are specified for each (group of) foto's
                        // these start with digits or a period
                        // In that case convert the first character after a space to uppercase

                        // Check for digits and periods
                        switch (trimmedString[0])
                        {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                            case '.':
                                // Check for a space
                                int spacePosition = trimmedString.IndexOf(' ');

                                // Space must be in the first part of the substring
                                if ((spacePosition > 0) && (spacePosition < 8))
                                {
                                    TotalString.Append(trimmedString.Substring(0, spacePosition) +
                                        ' ' +
                                        trimmedString[spacePosition + 1].ToString().ToUpper() +
                                    trimmedString.Substring(spacePosition + 2, trimmedString.Length - 2 - spacePosition));
                                }
                                else
                                {
                                    // Default action
                                    // Convert first character to uppercase
                                    if (trimmedString.Length > 1)
                                        TotalString.Append(trimmedString[0].ToString().ToUpper() + trimmedString.Substring(1, trimmedString.Length - 1));
                                    else
                                        if (trimmedString.Length > 0)
                                            TotalString.Append(trimmedString[0].ToString().ToUpper());
                                }
                                break;
                            default:
                                // Default action
                                // Convert first character to uppercase
                                if (trimmedString.Length > 1)
                                    TotalString.Append(trimmedString[0].ToString().ToUpper() + trimmedString.Substring(1, trimmedString.Length - 1));
                                else
                                    if (trimmedString.Length > 0)
                                        TotalString.Append(trimmedString[0].ToString().ToUpper());
                                break;
                        }
                    }
                    else
                        // Default action
                        // Convert first character to uppercase
                        if (trimmedString.Length > 1)
                            TotalString.Append(trimmedString[0].ToString().ToUpper() + trimmedString.Substring(1, trimmedString.Length - 1));
                        else
                            if (trimmedString.Length > 0)
                                TotalString.Append(trimmedString[0].ToString().ToUpper());

                    // Let each string end with a period
                    bool appendPeriod = false;
                    if (TotalString.Length > 0)
                        if ((TotalString[TotalString.Length - 1] != '.') && (TotalString[TotalString.Length - 1] != ':') && (TotalString[TotalString.Length - 1] != ';'))
                            appendPeriod = true;

                    if (appendPeriod)
                        TotalString.Append('.');

                    // Reinsert the break tag
                    TotalString.Append("<br />");
                }
            }
            catch (Exception exc)
            {
                ErrorString = "Ensure that <br /> is followed by an uppercase - " + exc.Message;
            }

            inputString = " " + TotalString.ToString().Substring(0, TotalString.Length - 6);
            inputString = inputString.Replace(" .", ".");
            inputString = inputString.Replace(",.", ".");
            inputString = inputString.Replace("<br />.<br />", "<br /><br />");

            if ((inputString[0].ToString() != inputString[0].ToString().ToUpper()) && inputString.Length > 1)
                inputString = inputString[0].ToString().ToUpper() + inputString.Substring(1, inputString.Length - 1);

            return inputString;
        }

        #region CorrectString
        public static string CorrectString(string inputString, bool doAbbreviations, string ErrorString)
        {
            string resultString = " " + inputString;

            // Incorrectly formatted HTML
            resultString = resultString.Replace("<br>", "<br />");
            resultString = resultString.Replace("<BR>", "<br />");

            // Remove double spaces
            resultString = resultString.Replace("  ", " ");

            // Convert quotes
            resultString = resultString.Replace("\"", "'");
            resultString = resultString.Replace("„", "'");
            resultString = resultString.Replace("“", "'");
            resultString = resultString.Replace("”", "'");
            resultString = resultString.Replace("‘", "'");
            resultString = resultString.Replace("’", "'");

            // Punctuation corrections
            resultString = resultString.Replace(":.", ".<br />");
            resultString = resultString.Replace(":>", "");
            resultString = resultString.Replace(": .", ":<br />");
            resultString = resultString.Replace(".;", ".");
            resultString = resultString.Replace(".:", ".");
            resultString = resultString.Replace("..", ". ");
            resultString = resultString.Replace(". .", ".<br />");
            resultString = resultString.Replace("-.", "-");
            resultString = resultString.Replace("+.", "+");
            resultString = resultString.Replace(" .", ".");
            resultString = resultString.Replace("!!!~.", "!");
            resultString = resultString.Replace("!~.", "!");
            resultString = resultString.Replace("!!.", "!");
            resultString = resultString.Replace("!!", "!");
            resultString = resultString.Replace("''", "'");
            resultString = resultString.Replace("!.", "!");

            resultString = resultString.Replace("&&", "");
            resultString = resultString.Replace(", , , , , , , , , , ", "");
            resultString = resultString.Replace(",.", ".");
            resultString = resultString.Replace(";.", ".");
            resultString = resultString.Replace(".-", "-");
            resultString = resultString.Replace("??", "?");
            resultString = resultString.Replace("~~", "");
            resultString = resultString.Replace("~", "");

            // Unnecessary new line
            resultString = resultString.Replace("<b>\r\n", "<b>");
            resultString = resultString.Replace("<b>\n\r", "<b>");

            // These are two new lines
            resultString = resultString.Replace("\n\r\n\r", "<br /><br />");
            resultString = resultString.Replace("\r\n\r\n", "<br /><br />");

            // This is a new line
            resultString = resultString.Replace("\n\r", "<br />");
            resultString = resultString.Replace("\r\n", "<br />");

            // Remove double spaces
            resultString = resultString.Replace("  ", " ");

            // This is an unnecessary whitespace
            resultString = resultString.Replace("<br /> .", "<br />");
            resultString = resultString.Replace(" <br />", "<br />");
            resultString = resultString.Replace("<br /> ", "<br />");

            resultString = resultString.Replace("<br />.", "<br />");

            // Convert "<br /> <br />" to "<br /><br />"
            resultString = resultString.Replace("<br /> <br />", "<br /><br />");
            // Convert "<br />'<br />" to "<br /><br />"
            resultString = resultString.Replace("<br />'<br />", "<br /><br />");
            // Convert ",<br />" to ".<br />"
            resultString = resultString.Replace(",<br />", ".<br />");
            // Convert ", <br />" to ".<br />"
            resultString = resultString.Replace(", <br />", ".<br />");

            resultString = resultString.Replace(".</p>.<br />", ".</p><p>");
            resultString = resultString.Replace("<br />&nbsp;<br />", "</p><p>");

            // Ensure that each : is followed by a space (but not for digital images containing timestamps or url's)
            if (resultString.Contains(":") && !resultString.Contains("GPS") && !resultString.Contains("http"))
                resultString = resultString.Replace(":", ": ");
            // Ensure that each ; is followed by a space (but not for digital images containing timestamps)
            if (resultString.Contains(";") && !resultString.Contains("GPS"))
                resultString = resultString.Replace(";", "; ");

            // Capitalize and cleanup
            resultString = Capitalize(resultString, ErrorString);

            // Random corrections
            resultString = resultString.Replace(" Aantekeningen:<br />", "<br />Aantekeningen:<br />");
            resultString = resultString.Replace(" A.G.I ", " AGI ");
            resultString = resultString.Replace(" A.G.I. ", " AGI ");
            resultString = resultString.Replace(" Data ", " data ");
            resultString = resultString.Replace(" e.d.", " en dergelijke");
            resultString = resultString.Replace(" ed.", " en dergelijke");
            resultString = resultString.Replace(" eerste kamer ", " Eerste Kamer ");
            resultString = resultString.Replace(" eronder", " er onder");
            resultString = resultString.Replace(" fa.", " firma ");
            resultString = resultString.Replace(" Fa.", " firma ");
            resultString = resultString.Replace(" Hr ", " heer ");
            resultString = resultString.Replace(" hr ", " heer ");
            resultString = resultString.Replace(" ir ", " ingenieur ");
            resultString = resultString.Replace(" Ir.", " Ingenieur ");
            resultString = resultString.Replace(" ir.", " Ingenieur ");
            resultString = resultString.Replace(" Ministerie", " ministerie");
            resultString = resultString.Replace(" ms. ", " M.S.");
            resultString = resultString.Replace(" neg ", " negatief ");
            resultString = resultString.Replace(" neg.", " negatief ");
            resultString = resultString.Replace(" Negatief ", " negatief ");
            resultString = resultString.Replace(" noord ", " Noord ");
            resultString = resultString.Replace(" noord", " Noord");
            resultString = resultString.Replace(" noord-", " Noord-");
            resultString = resultString.Replace(" noord.", " Noord.");
            resultString = resultString.Replace(" noord:", " Noord:");
            resultString = resultString.Replace(" nr.", " nummer ");
            resultString = resultString.Replace(" oost ", " Oost ");
            resultString = resultString.Replace(" oost", " Oost");
            resultString = resultString.Replace(" oost-", " Oost-");
            resultString = resultString.Replace(" oost.", " Oost.");
            resultString = resultString.Replace(" oost:", " Oost:");
            resultString = resultString.Replace(" open dag ", " Open Dag ");
            resultString = resultString.Replace(" opn. ", " opname ");
            resultString = resultString.Replace(" RW ", " RW");
            resultString = resultString.Replace(" rw ", " RW");
            resultString = resultString.Replace(" rw", " RW");
            resultString = resultString.Replace(" rw.", " RW");
            resultString = resultString.Replace(" Sluizen", " sluizen");
            resultString = resultString.Replace(" st ", " Sint ");
            resultString = resultString.Replace(" St ", " Sint ");
            resultString = resultString.Replace(" st. ", " Sint ");
            resultString = resultString.Replace(" St. ", " Sint ");
            resultString = resultString.Replace(" St.", " Sint ");
            resultString = resultString.Replace(" t/m", " tot en met");
            resultString = resultString.Replace(" t.bijvoorbeeld", " ten behoeve van");
            resultString = resultString.Replace(" trace ", " tracé ");
            resultString = resultString.Replace(" tweede kamer ", " Tweede Kamer ");
            resultString = resultString.Replace(" v ", " van ");
            resultString = resultString.Replace(" v/d", " van de");
            resultString = resultString.Replace(" Van ", " van ");
            resultString = resultString.Replace(" Van", " van");
            resultString = resultString.Replace(" west ", " West ");
            resultString = resultString.Replace(" west", " West");
            resultString = resultString.Replace(" west-", " West-");
            resultString = resultString.Replace(" west.", " West.");
            resultString = resultString.Replace(" west:", " West:");
            resultString = resultString.Replace(" zuid ", " Zuid ");
            resultString = resultString.Replace(" zuid", " Zuid");
            resultString = resultString.Replace(" zuid-", " Zuid-");
            resultString = resultString.Replace(" zuid.", " Zuid.");
            resultString = resultString.Replace(" zuid:", " Zuid:");
            resultString = resultString.Replace(" Zuidel richting", " Zuidelijke richting");
            resultString = resultString.Replace(" z.w.", " zuidwestelijke");
            resultString = resultString.Replace(" z.o.", " zuidoostelijke");
            resultString = resultString.Replace(" n.w.", " noordwestelijke");
            resultString = resultString.Replace(" n.o.", " noordoostelijke");
            resultString = resultString.Replace("<strong>hoogte:", "<strong>Hoogte:");
            resultString = resultString.Replace("</abbr>:", "</abbr>;");
            resultString = resultString.Replace("a.d.", " aan de ");
            resultString = resultString.Replace("a/d", " aan de ");
            resultString = resultString.Replace("aan zee", "aan Zee");
            resultString = resultString.Replace("ademanalyseapparaat", "ademanalyse apparaat");
            resultString = resultString.Replace("aeslandkering", "aeslantkering");
            resultString = resultString.Replace("afd ", "afdeling ");
            resultString = resultString.Replace("avondfoto's", "avond fotoserie");
            resultString = resultString.Replace("auto'snelweg", "autosnelweg");
            resultString = resultString.Replace("avondfotoserie", "avond fotoserie");
            resultString = resultString.Replace("b.v. ", "bijvoorbeeld ");
            resultString = resultString.Replace("bovenop", "boven op");
            resultString = resultString.Replace("BRAK", "brak");
            resultString = resultString.Replace("Rhenen.<br />Provincie : Gelderland", "Rhenen.<br />Provincie : Utrecht");
            resultString = resultString.Replace("d.m.v. ", "door middel van ");
            resultString = resultString.Replace("De Berging", "De berging");
            resultString = resultString.Replace("den haag", "Den Haag");
            resultString = resultString.Replace("den Helder", "Den Helder");
            resultString = resultString.Replace("den oever", "Den Oever");
            resultString = resultString.Replace("dhr.", "heer ");
            resultString = resultString.Replace("Dhr.", "heer ");
            resultString = resultString.Replace("'dienst informatie verwerking'", "'Dienst Informatie Verwerking'");
            resultString = resultString.Replace("dir ", "directie ");
            resultString = resultString.Replace("Dir ", "Directie ");
            resultString = resultString.Replace("Dir. Gen.", "Directeur Generaal");
            resultString = resultString.Replace("Dir.", "Directie ");
            resultString = resultString.Replace("dir.", "directie");
            resultString = resultString.Replace("Dmv", "Door middel van ");
            resultString = resultString.Replace("dmv", "door middel van");
            resultString = resultString.Replace("dos 2", "DOS II, de tegelmattenlegger");
            resultString = resultString.Replace("ederwerker", "edewerker");
            resultString = resultString.Replace("erachter", "er achter");
            resultString = resultString.Replace("erlangs", "er langs");
            resultString = resultString.Replace("ernaast", "er naast");
            resultString = resultString.Replace("etc.", "etcetera");
            resultString = resultString.Replace("fa ", "firma ");
            resultString = resultString.Replace("fa. ", "firma ");
            resultString = resultString.Replace("fa.", "firma ");
            resultString = resultString.Replace("foto's ", "fotoserie ");
            resultString = resultString.Replace("Foto's", "Fotoserie");
            resultString = resultString.Replace("Foto, s", "Fotoserie");
            resultString = resultString.Replace("foto's.", "fotoserie.");
            resultString = resultString.Replace("foto's:", "fotoserie:");
            resultString = resultString.Replace("fotoserie zijn", "fotoserie is");
            resultString = resultString.Replace("Gem ", "Gemeente ");
            resultString = resultString.Replace("gegevens :", "gegevens:");
            resultString = resultString.Replace("gem. ", "gemeente ");
            resultString = resultString.Replace("Gem.", "Gemeente ");
            resultString = resultString.Replace("gem.", "gemeente ");
            resultString = resultString.Replace("H I Ambacht", "H.I. Ambacht");
            resultString = resultString.Replace("H. I. Ambacht", "H.I. Ambacht");
            resultString = resultString.Replace("H. v. Holland", "Hoek van Holland");
            resultString = resultString.Replace("H.I.Ambacht", "H.I. Ambacht");
            resultString = resultString.Replace("H.I.D.", "HID");
            resultString = resultString.Replace("Hendrik-ido-Ambacht", "Hendrik-Ido-Ambacht");
            resultString = resultString.Replace("Hr.", "heer ");
            resultString = resultString.Replace("hydraulischekranen", "hydraulische kranen");
            resultString = resultString.Replace("hr.", "heer ");
            resultString = resultString.Replace("i.v.m.", "in verband met ");
            resultString = resultString.Replace("Ij", "IJ");
            resultString = resultString.Replace("incl. Deel", "inclusief deel");
            resultString = resultString.Replace("incl.", "inclusief ");
            resultString = resultString.Replace("incl.Deel", "inclusief deel");
            resultString = resultString.Replace("Ivm", "In verband met ");
            resultString = resultString.Replace("ivm", "in verband met ");
            resultString = resultString.Replace("Jvh-", "JVH_");
            resultString = resultString.Replace("jvh-", "JVH_");
            resultString = resultString.Replace("JVH-", "JVH_");
            resultString = resultString.Replace("jvh_", "JVH_");
            resultString = resultString.Replace("Jvh_", "JVH_");
            resultString = resultString.Replace("Leiderdorp.snelweg", "Leiderdorp; snelweg"); // tijdelijk
            resultString = resultString.Replace("luchtfoto kust", "luchtfotoserie kust");
            resultString = resultString.Replace("Luchtfoto kust", "Luchtfotoserie kust");
            resultString = resultString.Replace("luchtfoto; luchtfoto", "luchtfoto;");
            resultString = resultString.Replace("m.b.t.", "met betrekking tot ");
            resultString = resultString.Replace("m.b.v.", "met behulp van ");
            resultString = resultString.Replace("m.s.", "M.S. ");
            resultString = resultString.Replace("max.", "maximaal ");
            resultString = resultString.Replace("mbv", "met behulp van ");
            resultString = resultString.Replace("Mevr.", "mevrouw ");
            resultString = resultString.Replace("mevr.", "mevrouw ");
            resultString = resultString.Replace("Min. V&W", "ministerie van VenW");
            resultString = resultString.Replace("min. V&W", "ministerie van VenW");
            resultString = resultString.Replace("Min. van V&W", "ministerie van VenW");
            resultString = resultString.Replace("min. van V&W", "ministerie van VenW");
            resultString = resultString.Replace("Min: verkeer & waterstaat", "ministerie van VenW");
            resultString = resultString.Replace("ministerie vanVenW", "ministerie van VenW");
            resultString = resultString.Replace("Mvr", "mevrouw");
            resultString = resultString.Replace("n.a.v.", "naar aanleiding van ");
            resultString = resultString.Replace("N.Brabant", "Noord Brabant");
            resultString = resultString.Replace("N+Z", "Noord en Zuid");
            resultString = resultString.Replace("N+Z.zijde", "Noord en Zuid zijde");
            resultString = resultString.Replace("nederland", "Nederland");
            resultString = resultString.Replace("nl.", "namelijk");
            resultString = resultString.Replace("noord Holland", "Noord-Holland");
            resultString = resultString.Replace("Noord Holland", "Noord-Holland");
            resultString = resultString.Replace("Noord hollandskanaal", "Noord Hollandskanaal");
            resultString = resultString.Replace("noord-Holland", "Noord-Holland");
            resultString = resultString.Replace("nr .", "nummer ");
            resultString = resultString.Replace("O.a. ", "Onder andere ");
            resultString = resultString.Replace("o.a.", "onder andere ");
            resultString = resultString.Replace("ong.", "ongeveer ");
            resultString = resultString.Replace("OostKant", "Oostkant");
            resultString = resultString.Replace("Open dag", "Open Dag");
            resultString = resultString.Replace("opendag", "Open Dag");
            resultString = resultString.Replace("Opendag", "Open dag");
            resultString = resultString.Replace("opn.", "opname ");
            resultString = resultString.Replace("overzicht fotoserie", "overzicht foto's");
            resultString = resultString.Replace("overzichtfotoserie", "overzicht foto's");
            resultString = resultString.Replace("pag.", "pagina ");
            resultString = resultString.Replace("pijlerss ", "pijlers ");
            resultString = resultString.Replace("pijlersss ", "pijlers ");
            resultString = resultString.Replace("pijlerssss ", "pijlers ");
            resultString = resultString.Replace("Prof.", "Prof. ");
            resultString = resultString.Replace("Reddings artikel", "Reddingsartikel");
            resultString = resultString.Replace("reparatie's", "reparaties");
            resultString = resultString.Replace("reproductie's", "reproducties");
            resultString = resultString.Replace("reserve fotoserie", "reserve foto's");
            resultString = resultString.Replace("ri. ", "richting ");
            resultString = resultString.Replace("Ri.", "richting ");
            resultString = resultString.Replace("rijkswweg", "rijksweg");
            resultString = resultString.Replace("ringweg-Noord", "ringweg Noord");
            resultString = resultString.Replace("Rw ", "RW ");
            resultString = resultString.Replace("RWa", "rijksweg A");
            resultString = resultString.Replace("RWA", "rijksweg A");
            resultString = resultString.Replace("'s- ", "'s-");
            resultString = resultString.Replace("Secr. Gen. Van", "Secretaris Generaal van");
            resultString = resultString.Replace("s'Gravenhage", "Den Haag");
            resultString = resultString.Replace("situatie's", "situaties");
            resultString = resultString.Replace("snacht's", "'s nachts");
            resultString = resultString.Replace("s'nachts", "'s nachts");
            resultString = resultString.Replace("t. b. v.", "ten behoeve van ");
            resultString = resultString.Replace("t.b.v.", "ten behoeve van ");
            resultString = resultString.Replace("t.bijvoorbeeld ", "ten behoeve van ");
            resultString = resultString.Replace("t.g.v.", "ten gevolge van");
            resultString = resultString.Replace("t.h.v.", "ter hoogte van");
            resultString = resultString.Replace("t.p.v. ", "ter plaatse van ");
            resultString = resultString.Replace("T/m", "tot en met");
            resultString = resultString.Replace("tbv", "ten behoeve van");
            resultString = resultString.Replace("tgv", "ten gevolge van");
            resultString = resultString.Replace("thv", "ter hoogte van");
            resultString = resultString.Replace("Tot en met.", "Tot en met ");
            resultString = resultString.Replace("tot en met.", "tot en met ");
            resultString = resultString.Replace("tpv ", "ter plaatse van ");
            resultString = resultString.Replace("trac?", "tracé");
            resultString = resultString.Replace("Trac?", "Tracé");
            resultString = resultString.Replace("Trac�ziekenhuis", "Tracé ziekenhuis");
            resultString = resultString.Replace("tpv ", "ter plaatse van ");
            resultString = resultString.Replace("tweede wereldoorlog", "Tweede Wereldoorlog");
            resultString = resultString.Replace("tweede-kamer", "Tweede Kamer");
            resultString = resultString.Replace("V en W", "VenW");
            resultString = resultString.Replace("V&W", "VenW");
            resultString = resultString.Replace("v.l.n.r.", "van links naar rechts ");
            resultString = resultString.Replace("van Brienenoord", "Van Brienenoord");
            resultString = resultString.Replace("van brienenoord", "Van Brienenoord");
            resultString = resultString.Replace("verkeer en waterstaat", "Verkeer en Waterstaat");
            resultString = resultString.Replace("voorlangs", "voor langs");
            resultString = resultString.Replace("Weersomstandigheden :", "Weersomstandigheden:");
            resultString = resultString.Replace("zg. ", "zogenaamde ");
            resultString = resultString.Replace("zuid Beveland", "Zuid-Beveland");
            resultString = resultString.Replace("zuid beveland", "Zuid-Beveland");
            resultString = resultString.Replace("zuid Holland", "Zuid-Holland");
            resultString = resultString.Replace("Zuid Holland", "Zuid-Holland");
            resultString = resultString.Replace("zuid Willemsvaart", "Zuid-Willemsvaart");
            resultString = resultString.Replace("zuid- Willemsvaart", "Zuid-Willemsvaart");
            resultString = resultString.Replace("zuid-Beveland", "Zuid-Beveland");
            resultString = resultString.Replace("zuid-Holland", "Zuid-Holland");
            resultString = resultString.Replace("zuid-Holland", "Zuid-Holland");
            resultString = resultString.Replace("zuid-Willemsvaart", "Zuid-Willemsvaart");
            resultString = resultString.Replace("zw / w", "zwart/wit");
            resultString = resultString.Replace("zw /w", "zwart/wit");
            resultString = resultString.Replace("zw/w", "zwart/wit");
            resultString = resultString.Replace("zwaar transportwagens, en", "zware transportwagens en");
            resultString = resultString.Replace("zwart/Wit", "zwart/wit");
            resultString = resultString.Replace("Zwart/Wit", "zwart/wit");

            // Remove double spaces and trim result
            resultString = resultString.Replace("  ", " ");
            resultString = resultString.Trim();

            // Separate change that must be done later
            resultString = resultString.Replace("min. ", "minister ");
            resultString = resultString.Replace("Min. ", "minister ");
            resultString = resultString.Replace("beeldbank.RWS.nl", "beeldbank.rws.nl");
            resultString = resultString.Replace("beeldbank.rijkswaterstaat.nl", "beeldbank.rws.nl");
            resultString = resultString.Replace(".namelijk", ".nl");
            resultString = resultString.Replace("www.Rijkswaterstaat.nl", "www.rijkswaterstaat.nl");
            
            // Add language switch tag
            // and correct this piece of text
            resultString = resultString.Replace(" <span lang='en' xml: lang='en'> <span lang='en' xml: lang='en'> <span lang='en' xml: lang='en'> <span lang='en' xml: lang='en'>'Controlling the boom'</span></span></span></span>", " <span lang='en' xml:lang='en'>'Controlling the boom'</span>");
            resultString = resultString.Replace("Reddings oevening pre sea", "Reddings oefening <span lang='en' xml:lang='en'>Pre Sea</span>");

            // Correct uppercasing if possible
            resultString = resultString.Replace(". a", ". A");
            resultString = resultString.Replace(". b", ". B");
            resultString = resultString.Replace(". c", ". C");
            resultString = resultString.Replace(". d", ". D");
            resultString = resultString.Replace(". e", ". E");
            resultString = resultString.Replace(". f", ". F");
            resultString = resultString.Replace(". g", ". G");
            resultString = resultString.Replace(". h", ". H");
            resultString = resultString.Replace(". i", ". I");
            resultString = resultString.Replace(". j", ". J");
            resultString = resultString.Replace(". k", ". K");
            resultString = resultString.Replace(". l", ". L");
            resultString = resultString.Replace(". m", ". M");
            resultString = resultString.Replace(". n", ". N");
            resultString = resultString.Replace(". o", ". O");
            resultString = resultString.Replace(". p", ". P");
            resultString = resultString.Replace(". q", ". Q");
            resultString = resultString.Replace(". r", ". R");
            resultString = resultString.Replace(". s", ". S");
            resultString = resultString.Replace(". t", ". T");
            resultString = resultString.Replace(". u", ". U");
            resultString = resultString.Replace(". v", ". V");
            resultString = resultString.Replace(". w", ". W");
            resultString = resultString.Replace(". x", ". X");
            resultString = resultString.Replace(". y", ". Y");
            resultString = resultString.Replace(". z", ". Z");

            // Remove double spaces
            resultString = resultString.Replace("  ", " ");

            // Capitalize and cleanup again
            resultString = Capitalize(resultString, ErrorString);

            // Corrections after the final capitalize
            resultString = resultString.Replace(" Tot en met.", " tot en met ");
            resultString = resultString.Replace(" Overzichten", " overzichten");
            resultString = resultString.Replace("historisch Archief", "Historisch archief");

            // Correct endings
            resultString = resultString.Trim();
            if (resultString.EndsWith("</p>."))
                resultString = resultString.Substring(0, resultString.Length - 1);
            if (resultString.EndsWith("<br /><br />."))
                resultString = resultString.Substring(0, resultString.Length - 13);
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br />."))
                resultString = resultString.Substring(0, resultString.Length - 7);
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br />"))
                resultString = resultString.Substring(0, resultString.Length - 6);
            resultString = resultString.Trim();
            if (resultString.EndsWith("~~"))
                resultString = resultString.Substring(0, resultString.Length - 2);
            resultString = resultString.Trim();
            if (resultString.EndsWith("~"))
                resultString = resultString.Substring(0, resultString.Length - 1);
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br /> i"))
                resultString = resultString.Substring(0, resultString.Length - 8);
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br />Mapnummer."))
                resultString = resultString.Replace("<br />Mapnummer.", "");
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br />Mapnummer ."))
                resultString = resultString.Replace("<br />Mapnummer .", "");
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br />Aantekeningen :"))
                resultString = resultString.Replace("<br />Aantekeningen :", "");
            resultString = resultString.Trim();
            if (resultString.EndsWith("<br />Aantekeningen :"))
                resultString = resultString.Replace("<br />Aantekeningen :", "");
            resultString = resultString.Trim();
            if (resultString.EndsWith("Aantekeningen :"))
                resultString = resultString.Replace("Aantekeningen :", "");
            resultString = resultString.Trim();
            if (resultString.EndsWith(":"))
                resultString = resultString.Substring(0, resultString.Length - 1) + ".";
            resultString = resultString.Trim();
            // NOTE: Twice since this does occur sometimes
            if (resultString.EndsWith("<br />"))
                resultString = resultString.Substring(0, resultString.Length - 6);

            // Google maps corrections
            resultString = resultString.Replace("Differential Correction : 0", "");

            if (resultString.Contains("Tijdstip"))
            {
                string firstpart = resultString.Substring(0, resultString.IndexOf("Tijdstip"));
                string secondpart = resultString.Substring(resultString.IndexOf("Tijdstip"), 40);
                string thirdpart = resultString.Substring(resultString.IndexOf("Tijdstip") + 40, resultString.Length - resultString.IndexOf("Tijdstip") - 40);
                secondpart = secondpart.Replace(": 0", ":00");
                secondpart = secondpart.Replace(": 1", ":01");
                secondpart = secondpart.Replace(": 2", ":02");
                secondpart = secondpart.Replace(": 3", ":03");
                secondpart = secondpart.Replace(": 4", ":04");
                secondpart = secondpart.Replace(": 5", ":05");
                secondpart = secondpart.Replace(": 6", ":06");
                secondpart = secondpart.Replace(": 7", ":07");
                secondpart = secondpart.Replace(": 8", ":08");
                secondpart = secondpart.Replace(": 9", ":09");
                resultString = firstpart + secondpart + thirdpart;
            }

            if (doAbbreviations)
            {
                // Precede the string with a space character to ensure that starting with an abbreviation is also processed
                resultString = " " + resultString;
                resultString = resultString.Replace(" ADCP", " <abbr lang='en' title='Acoustic Doppler Current Profiler'>ADCP</abbr>");
                resultString = resultString.Replace(" AGI", " <abbr title='Adviesdienst Geo-Informatie en ICT'>AGI</abbr>");
                resultString = resultString.Replace("-AGI", "-<abbr title='Adviesdienst Geo-Informatie en ICT'>AGI</abbr>");
                resultString = resultString.Replace(" AKI", " <abbr title='Automatische KnipperlichtInstallatie '>AKI</abbr>");
                resultString = resultString.Replace(" A.K.I.", " <abbr title='Automatische KnipperlichtInstallatie '>AKI</abbr>");
                resultString = resultString.Replace("(AKI)", " (<abbr title='Automatische KnipperlichtInstallatie '>AKI</abbr>)");
                resultString = resultString.Replace(" AVD", " <abbr title='Algemene Verkeersdienst'>AVD</abbr>");
                resultString = resultString.Replace(" AVV", " <abbr title='Adviesdienst Verkeer en Vervoer'>AVV</abbr>");
                resultString = resultString.Replace(" AZU", " <abbr title='Academisch Ziekenhuis Utrecht'>AZU</abbr>");
                resultString = resultString.Replace(" CAD", " <abbr lang='en' title='Computer Aided Design'>CAD</abbr>");
                resultString = resultString.Replace(" CS", " <abbr title='Centraal Station'>CS</abbr>");
                resultString = resultString.Replace(" Deltar ", " <abbr title='Delta Getij Analogon Rekenmachine'>Deltar</abbr> ");
                resultString = resultString.Replace(" DG ", " <abbr title='Directeur Generaal'>DG</abbr> ");
                resultString = resultString.Replace(" DGSM", " <abbr title='Directoraat-generaal Scheepvaart en Maritieme Zaken'>DGSM</abbr>");
                resultString = resultString.Replace(" DGV", " <abbr title='Directoraat-Generaal van het Verkeer'>DGV</abbr>");
                resultString = resultString.Replace("(DGV)", "(<abbr title='Directoraat-Generaal van het Verkeer'>DGV</abbr>)");
                resultString = resultString.Replace(" DGW", " <abbr title='Dienst Getijdewateren'>DGW</abbr>");
                resultString = resultString.Replace(" DISK", " <abbr title='Data Informatie Systeem Kunstwerken'>DISK</abbr>");
                resultString = resultString.Replace(" DIV", " <abbr title='Dienst Informatie Verwerking'>DIV</abbr>");
                resultString = resultString.Replace(" DID", " <abbr title='Data-ICT-Dienst'>DID</abbr>");
                resultString = resultString.Replace(" DVK", " <abbr title='Dienst Verkeerskunde'>DVK</abbr>");
                resultString = resultString.Replace(" DWW", " <abbr title='Dienst Weg- en Waterbouwkunde'>DWW</abbr>");
                resultString = resultString.Replace(" EEG", " <abbr title='Europese Economische Gemeenschap'>EEG</abbr>");
                resultString = resultString.Replace(" E.P.", " <abbr lang='en' title='Eco-Point'>E.P.</abbr>");
                resultString = resultString.Replace(" EPZ", " <abbr title='Elektriciteits-Produktiemaatschappij Zuid-Nederland'>EPZ</abbr>");
                resultString = resultString.Replace(" FAIS", " <abbr title='Financiëel Administratie- en Informatiesysteem'>FAIS</abbr>");
                resultString = resultString.Replace(" GOR", " <abbr title='Groepsondernemingsraad'>GOR</abbr>");
                resultString = resultString.Replace(" GSM", " <abbr lang='en' title='Global System for Mobile communications'>GSM</abbr>");
                resultString = resultString.Replace(" GPS", " <abbr lang='en' title='Global Positioning Satellite'>GPS</abbr>");
                resultString = resultString.Replace(">GPS:", "><abbr lang='en' title='Global Positioning Satellite'>GPS</abbr>:");
                resultString = resultString.Replace(" HID", " <abbr title='HoofdIngenieur Directeur'>HID</abbr>");
                resultString = resultString.Replace(" HS ", " <abbr title='Hollands Spoor'>HS</abbr> ");
                resultString = resultString.Replace(" HSL", " <abbr title='Hoge Snelheids Lijn'>HSL</abbr>");
                resultString = resultString.Replace(" IBS", " <abbr title='InformatieBeleid Strategie en beleid'>IBS</abbr>");
                resultString = resultString.Replace(" ICIM", " <abbr title='Interdepartementale Commissie Integratiebeleid Minderheden'>ICIM</abbr>");
                resultString = resultString.Replace(" IDVV", " <abbr title='Impuls Dynamisch Verkeersmanagement Vaarwegen'>IDVV</abbr>");
                resultString = resultString.Replace("(IDVV)", "(<abbr title='Impuls Dynamisch Verkeersmanagement Vaarwegen'>IDVV</abbr>)");
                resultString = resultString.Replace(" INP", " <abbr lang='en' title='Integrated North-Sea Programme'>INP</abbr>");
                resultString = resultString.Replace(" KLPD", " <abbr title='Korps Landelijke Politiediensten'>KLPD</abbr>");
                resultString = resultString.Replace(" LPG", " <abbr lang='en' title='Liquefied Petroleum Gas'>LPG</abbr>");
                resultString = resultString.Replace(" KLM", " <abbr title='Koninklijke Luchtvaart Maatschappij'>KLM</abbr>");
                resultString = resultString.Replace(" KNMI", " <abbr title='Koninklijk Nederlands Meteorologisch Instituut'>KNMI</abbr>");
                resultString = resultString.Replace(" MER", " <abbr title='Milieu Effect Rapportage'>MER</abbr>");
                resultString = resultString.Replace(" MKD", " <abbr title='Meetkundige Dienst'>MKD</abbr>");
                resultString = resultString.Replace(" MOMARO", " <abbr title='Modernisering Maasroute'>MOMARO</abbr>");
                resultString = resultString.Replace(" NAM", " <abbr title='Nederlandse Aardolie Maatschappij'>NAM</abbr>");
                resultString = resultString.Replace(" NAP", " <abbr title='Normaal Amsterdams Peil'>NAP</abbr>");
                resultString = resultString.Replace(" NIS", " <abbr title='Netwerk Informatie Systeem'>NIS</abbr>");
                resultString = resultString.Replace(" NOS", " <abbr title='Nederlandse Omroep Stichting'>NOS</abbr>");
                resultString = resultString.Replace(" RDM", " <abbr title='Rotterdamsche Droogdok Maatschappij '>RDM</abbr>");
                resultString = resultString.Replace(" RDS", " <abbr title='Radio Data Systeem'>RDS</abbr>");
                resultString = resultString.Replace(" RDW", " <abbr title='(Rijks)Dienst (voor het) Wegverkeer'>RDW</abbr>");
                resultString = resultString.Replace("(RIKZ)", "(<abbr title='Rijksinstituut voor Kust en Zee'>RIKZ</abbr>)");
                resultString = resultString.Replace(" RIKZ", " <abbr title='Rijksinstituut voor Kust en Zee'>RIKZ</abbr>");
                resultString = resultString.Replace("-RIKZ", "-<abbr title='Rijksinstituut voor Kust en Zee'>RIKZ</abbr>");
                resultString = resultString.Replace(" RIVO", " <abbr title='Rijksinstituut voor visserijonderzoek'>RIVO</abbr>");
                resultString = resultString.Replace("(RIZA)", "(<abbr title='Rijksinstituut voor Integraal Zoetwaterbeheer en Afvalwaterbehandeling'>RIZA</abbr>)");
                resultString = resultString.Replace(" RIZA", " <abbr title='Rijksinstituut voor Integraal Zoetwaterbeheer en Afvalwaterbehandeling'>RIZA</abbr>");
                resultString = resultString.Replace("/RIZA", "/<abbr title='Rijksinstituut voor Integraal Zoetwaterbeheer en Afvalwaterbehandeling'>RIZA</abbr>");
                resultString = resultString.Replace("(RWS)", "(<abbr title='Rijkswaterstaat'>RWS</abbr>)");
                resultString = resultString.Replace(" RWS", " <abbr title='Rijkswaterstaat'>RWS</abbr>");
                resultString = resultString.Replace("(RVI)", "(<abbr title='Radio Verkeersinformatie'>RVI</abbr>)");
                resultString = resultString.Replace(" RVI", " <abbr title='Rijksverkeersinspectie'>RVI</abbr>");
                resultString = resultString.Replace(" SCC", " <abbr title='Schelde Coordinatie Centrum'>SCC</abbr>");
                resultString = resultString.Replace("(SBW)", "(<abbr title='Sterkte en Belastingen Waterkeringen'>SBW</abbr>)");
                resultString = resultString.Replace(" SBW", " <abbr title='Sterkte en Belastingen Waterkeringen'>SBW</abbr>");
                resultString = resultString.Replace(" SG ", " <abbr title='Secretaris Generaal'>SG</abbr> ");
                resultString = resultString.Replace(" TMC", " <abbr title='Traffic Message Channel'>TMC</abbr>");
                resultString = resultString.Replace(" TKL", " <abbr title='Toetsing KustLijn'>TKL</abbr>");
                resultString = resultString.Replace("(TKL)", "(<abbr title='Toetsing KustLijn'>TKL</abbr>)");
                resultString = resultString.Replace(" TNO", " <abbr title='Toegepast Natuurwetenschappelijk Onderzoek'>TNO</abbr>");
                resultString = resultString.Replace(" VARI", " <abbr title='VAanplein - RIdderkerk'>VARI</abbr>");
                resultString = resultString.Replace(" VCM", " <abbr title='Verkeersmanagement Centrale van Morgen'>VCM</abbr>");
                resultString = resultString.Replace("(VCM)", "(<abbr title='Verkeersmanagement Centrale van Morgen'>VCM</abbr>)");
                resultString = resultString.Replace(" VCZ", " <abbr title='Verwerkings Centrum Zierikzee'>VCZ</abbr>");                
                resultString = resultString.Replace(" VenW", " <abbr title='Verkeer en Waterstaat'>VenW</abbr>");
                resultString = resultString.Replace("(VenW)", "(<abbr title='Verkeer en Waterstaat'>VenW</abbr>)");
                resultString = resultString.Replace(" VENW", " <abbr title='Verkeer en Waterstaat'>VenW</abbr>");
                resultString = resultString.Replace("(VENW)", "(<abbr title='Verkeer en Waterstaat'>VenW</abbr>)");
                resultString = resultString.Replace(" VOC", " <abbr title='Verenigde Oost-Indische Compagnie'>VOC</abbr>");
                resultString = resultString.Replace(" VOF", " <abbr title='Venootschap Onder Firma'>VOF</abbr>");
                resultString = resultString.Replace(" V&W", " <abbr title='Verkeer en Waterstaat'>VenW</abbr>");
                resultString = resultString.Replace("(V&W)", "(<abbr title='Verkeer en Waterstaat'>VenW</abbr>)");
                resultString = resultString.Replace(" VZZ", " <abbr title='Vereniging voor Zoogdierkunde en Zoogdierbescherming'>VZZ</abbr>");
                resultString = resultString.Replace(" WBD", " <abbr title='Wegbouwkundige Dienst'>WBD</abbr>");
                resultString = resultString.Replace(" WED", " <abbr title='Wet Economische Delicten'>WED</abbr>");
                resultString = resultString.Replace(" WTI", " <abbr title='Wettelijk Toets Instrumentarium'>WTI</abbr>");
                resultString = resultString.Replace("(WTI)", "(<abbr title='Wettelijk Toets Instrumentarium'>WTI</abbr>)");
                resultString = resultString.Replace(" ZOAB", " <abbr title='Zeer Open Asfalt Beton'>ZOAB</abbr>");

                resultString = resultString.Replace("<abbr title='Verkeersmanagement Centrale van Morgen'>(VCM)</abbr>)", "(<abbr title='Verkeersmanagement Centrale van Morgen'>VCM</abbr>)");
                resultString = resultString.Replace("<abbr title='Acoustic Doppler Current Profiler'>ADCP</abbr>", "<abbr lang='en' title='Acoustic Doppler Current Profiler'>ADCP</abbr>");
                resultString = resultString.Replace("<abbr title='Computer Aided Design'>CAD</abbr>", "<abbr lang='en' title='Computer Aided Design'>CAD</abbr>");
                resultString = resultString.Replace("<abbr title='Global System for Mobile communications'>GSM</abbr>", "<abbr lang='en' title='Global System for Mobile communications'>GSM</abbr>");
                resultString = resultString.Replace("<abbr title='Global Positioning Satellite'>GPS</abbr>", "<abbr lang='en' title='Global Positioning Satellite'>GPS</abbr>");
                resultString = resultString.Replace("<abbr title='Integrated North-Sea Programme'>INP</abbr>", "<abbr lang='en' title='Integrated North-Sea Programme'>INP</abbr>");
                resultString = resultString.Replace("<abbr title='Liquefied Petroleum Gas'>LPG</abbr>", "<abbr lang='en' title='Liquefied Petroleum Gas'>LPG</abbr>");
                resultString = resultString.Replace("<abbr title='vereniging voor Zoogdierkunde en Zoogdierbescherming'>VZZ</abbr>", "<abbr title='Vereniging voor Zoogdierkunde en Zoogdierbescherming'>VZZ</abbr>");
            }
            else
            {
                // Titels don't end with a period
                resultString = resultString.Trim();
                if (resultString.EndsWith("."))
                    resultString = resultString.Substring(0, resultString.Length - 1);
            }

            // Convert "<b>" to "<strong>"
            if (resultString.Contains("<b>"))
                resultString = resultString.Replace("<b>", "<strong>");
            // Convert "</b>" to "</strong>"
            if (resultString.Contains("</b>"))
                resultString = resultString.Replace("</b>", "</strong>");

            // Remove double spaces
            if (resultString.Contains("  "))
                resultString = resultString.Replace("  ", " ");

            // Correct empty resultstring
            if ((resultString.Trim().Length == 0))
            {
                if (doAbbreviations)
                    resultString = "Geen omschrijving bekend.";
                else
                    resultString = "Geen titel bekend.";
            }

            // First trim the result
            resultString = resultString.Trim();

            bool addBeginEndParagraphTag = false;

            if (resultString.Contains(".<br />Technische "))
            {
                resultString = resultString.Replace(".<br />Technische ", ".</p><p>Technische ");
                addBeginEndParagraphTag = true;
            }

            if (resultString.Contains(".<br />Weersomstandigheden"))
            {
                resultString = resultString.Replace(".<br />Weersomstandigheden", ".</p><p>Weersomstandigheden");
                addBeginEndParagraphTag = true;
            }

            if (resultString.Contains(".<br />Aantekeningen"))
            {
                resultString = resultString.Replace(".<br />Aantekeningen", ".</p><p>Aantekeningen");
                addBeginEndParagraphTag = true;
            }

            // Convert <br /><br /> into paragraph change
            if (resultString.Contains("<br /><br />"))
            {
                // All double breaks are converted to a paragraph switch
                resultString = resultString.Replace("<br /><br />", "</p><p>");
                addBeginEndParagraphTag = true;
            }

            if (resultString.Contains("</p><p>"))
                addBeginEndParagraphTag = true;

            if (addBeginEndParagraphTag)
            {
                // Check to see whether the description starts with <p>
                if (!resultString.StartsWith("<p>"))
                    // Make sure that we start with a <p> tag
                    resultString = "<p>" + resultString;
                // Check to see whether the description end with </p>
                if (!resultString.EndsWith("</p>"))
                    // Make sure that we end with a </p> tag
                    resultString = resultString + "</p>";
            }
            return resultString.Trim();
        }
        #endregion

        #region convertHtmlStringToAltTag
        public static string convertHtmlStringToAltTag(string inputString, string ErrorString)
        {
            if (String.IsNullOrEmpty(inputString))
            {
                return inputString;
            }

            string omschrijving = inputString;

            string regex = "[^^A-Za-z0-9]";
            System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace
                | System.Text.RegularExpressions.RegexOptions.Multiline)
                | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);

            string retVal = "";

            //
            int lokPeriod = -1;
            int increment = 50;
            bool stop = false;
            if (!String.IsNullOrEmpty(omschrijving))
            {
                while ((lokPeriod <= 0) && !stop)
                {
                    if (omschrijving.Length > increment)
                        lokPeriod = omschrijving.Substring(0, increment).LastIndexOf('.');

                    if ((omschrijving.Length > increment) && (lokPeriod < increment) && (lokPeriod > 0))
                        stop = true;
                    else
                        if (omschrijving.Length <= increment)
                            stop = true;
                    increment += 30;
                }
                if (lokPeriod > 0)
                    omschrijving = omschrijving.Substring(0, lokPeriod + 1);

                if (omschrijving.LastIndexOf('.') < 50)
                {
                    System.StringSplitOptions splitOptions = System.StringSplitOptions.RemoveEmptyEntries;
                    String[] newLine = { "\n" };
                    retVal = reg.Replace(StripHTMLTags(omschrijving.Replace("<br />", " ")).Replace("GPS", "\n").Replace("\r", "").Split(newLine, splitOptions)[0], " ").Trim(' ');
                }
                else
                    retVal = reg.Replace(StripHTMLTags(omschrijving.Replace("<br />", " ")).Replace("GPS", ".").Replace("\r", "").Replace("\n", ".").Split('.')[0], " ").Trim(' ');
            }
            else
                retVal = "";

            return retVal.Replace("  ", " ").Replace(" '", " ").Replace("; ", " ").Replace("  ", " ");
        }

        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        public static string StripHTMLTags(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }
        #endregion
    }
}
